/**
 * Created by Administrator on 2016/10/11.
 */
app.controller('MapCtrl', ['$scope', '$rootScope', 'httpService', 'wtmapService', '$filter', '$compile', '$localStorage',
    function($scope, $rootScope, httpService, wtmapService, $filter, $compile, $localStorage){


        //$scope.num = $filter('currency')(123534);

        //$scope.mapinstance=null;

        $scope.mapheight = 850;
        $scope.mapService = wtmapService;

        $scope.initialayer = false;
        $scope.isPopup = false;
        $scope.mapobj = null;
        $scope.mapcadObj = null;
        $scope.selecedresults = [];
        $scope.wtSys = wtSys;
        $scope.toolItems = [{itemName : "Point", title : "点拾取", imgName : "D_点拾取.png"},
            {itemName : "Rectangle", title : "矩形拾取", imgName : "D_矩形拾取.png"},
            {itemName : "Polygon", title : "多边形拾取", imgName : "D_多边形拾取.png"}
        ];

        $scope.pointLayers = [
            {
                lyrServerid : "0",
                layerName : "隐患点",
                layerFilter : "unitid" + $localStorage.userInfo.unitid,
                visibility : true,
                isDraw : false,
                selectedDatas : []
            }
            //{ lyrServerid: "2", layerName: "隐患点", layerFilter: "unitid="+$localStorage.userInfo.unitid,isDraw:true, requestUrl:"/danger?page=1&limit=100000", uniqueValues:{ valueField:"DangerStatus", renderInfos:renderInfo }, featuresData: [] }
        ];


        /*
         显隐图层
         */
        $scope.layerchecked = function(id, $event){


            if(id == "factorylry"){

                var lyr = $scope.mapobj.appMap.getLayer(id);
                if(lyr)
                    lyr.setVisibility($event.target.checked);
            }
            else {
                var lyr1 = $scope.mapcadObj.appMap.getLayer(id);
                if(lyr1)
                    lyr1.setVisibility($event.target.checked);
            }


        };

        //一级地理地图加载  使用Intialed回调是为了保证地图id已绑定值
        $scope.mapIntialed = function(scope){

            var promise = wtmapService.intialMap('map1', false, 0); //同步调用，获取承诺接口
            promise.then(function(mapobj){

                $scope.mapobj = mapobj;




            }, function(data){
                //调用承诺接口reject();
            });

        };

        //公司点击事件
        $scope.fatoryclick = function capitalclick(e){

            //var layer = $scope.mapobj.appMap.getLayer("factorylry");
            //layer.clear();

            var x = e.mapPoint.x;
            var y = e.mapPoint.y;

            var LngLat = $scope.mapobj.xyToLngLat(x, y);

            $scope.mapobj.zoomTobyXY(LngLat[0], LngLat[1], 16);

            $scope.isPopup = true;
            $scope.$apply();


            $("#factorylry").attr("disabled", true);
            $("#checkpoint").attr("disabled", false);
            $("#danger").attr("disabled", false);

        };


        //二级CAD地图加载  使用Intialed回调是为了保证地图id已绑定值
        $scope.mapcadIntialed = function(scope){

            var promise = wtmapService.intialMap('map2', false, 0, $rootScope.arcgisConfig.lods); //同步调用，获取承诺接口
            promise.then(function(mapobj){

                $scope.mapcadObj = mapobj;
                $scope.mapcadObj.addDynamicMap($rootScope.arcgisConfig.baseMap);

                var flyr = $scope.mapcadObj.addFeatureLayer("设备点", {
                    showInfoTemplate : false,
                    definitionExpression : "unitid=" + $localStorage.userInfo.unitid
                });

                //flyr.infoTemplate.setTitle("属性信息");
                //flyr.infoTemplate.setContent("编号:${code}");


                var dangerlyr = $scope.mapcadObj.addFeatureLayer("隐患点", {
                    showInfoTemplate : false,
                    definitionExpression : "unitid=" + $localStorage.userInfo.unitid
                });
                $scope.mapcadObj.appMap.infoWindow.setTitle("属性信息");

                //设置属性信息
                dangerlyr.on("click", function(evt){
                    $scope.ShowInfo(evt);
                });

                flyr.on("click", function(evt){
                    $scope.ShowInfo(evt);
                });


                $scope.mapcadObj.zoomTobyXY(114307.451, 145762.144, 15);

            }, function(data){
                //调用承诺接口reject();
            });
        };


        /*
         设置显示信息
         **/
        $scope.ShowInfo = function(evt){


            var map = $scope.mapcadObj.appMap;

            if(evt.graphic._layer.name == "设备点"){
                map.infoWindow.setContent("编号:" + evt.graphic.attributes.code);
                map.infoWindow.show(evt.mapPoint, map.getInfoWindowAnchor(evt.screenPoint));
            }
            else if(evt.graphic._layer.name == "隐患点"){
                var path = '/danger/' + evt.graphic.attributes.id;
                httpService.request("GET", path, null, function(result){

                    var Model = result.data;

                    Model.MissionLevel = $filter("getEmunName")(wtSys.Emun.MissionLevel, Model.MissionLevel);
                    Model.ServicingCode = Model.ServicingCode ? Model.ServicingCode : "无";
                    var strTemp = "隐患编号:" + Model.Code + "</br>" +
                        "上报时间:" + moment(Model.CreateTime).format(wtSys.Emun.DateFormat.DateTime24) + "</br>" +
                        "上报人员:" + Model.HandlerName + "</br>" +
                        "紧急程度:" + Model.MissionLevel + "</br>" +
                        "隐患类型:" + Model.DangerType + "</br>" +
                        "隐患主题:" + Model.Name + "</br>" +

                        "隐患描述:" + Model.Note + "</br>" +
                        "维修任务编号: <a href='javascript:void(0)'   id='whnum' >" + Model.ServicingCode + "</a></br>";

                    map.infoWindow.setContent(strTemp);
                    map.infoWindow.show(evt.mapPoint, map.getInfoWindowAnchor(evt.screenPoint));

                    $('#whnum').on('click', function(){
                        $scope.detail($('#whnum').text());
                    });

                });
            }

        }
        //展示维修任务概况详情
        $scope.detail = function(detailId){

            if(detailId){
                var path = '/servicing/' + detailId;
                httpService.request("GET", path, null, function(result){

                    $('#detailModal').modal('show');

                    $scope.servicingDetailModel = result.data;
                });
            }

        }

        /*
         返回第一层
         */
        $scope.goback = function(scope){

            var x = 114.09984904;
            var y = 22.54501255;
            $scope.isPopup = false;

            setTimeout(function(){
                $scope.mapobj.zoomTobyXY(x, y, 15);
            }, 500);


            $("#factorylry").attr("disabled", false);
            $("#checkpoint").attr("disabled", true);
            $("#danger").attr("disabled", true);

        }


        $scope.testclick1 = function(){


            $scope.pointLayers[1].selectedDatas = ['7467b00638dd4a7abfe1d0aa06751917', '0ba4c492755746b2add4a3b2c9ee8c40'];

            $scope.initialayer = true;
            $('#editModal').modal('show');
        }


    }]);





